<?php
namespace app\common\logic;

use app\common\library\exception\ApiException;
use app\common\library\Helper\DateTime;
use app\common\library\MemberVo;
use app\common\library\Upload;
use app\common\model\Car;
use app\common\model\CarImages;
use app\common\model\MaintainRecord;
use app\common\model\MaintainRecordImage;
use app\common\model\Member;
use app\common\model\Order;
use think\db\Query;
use think\Env;


class MaintainLogic extends BaseLogic
{
    public $model;
    public $imageModel;

    public function assignModel()
    {
        $this->model          = new MaintainRecord();
        $this->imageModel          = new MaintainRecordImage();
    }

    public function handleSearch(Query $query, array $params): Query
    {
        if (isset($params['keyword']) && !empty($params['keyword'])) {
            $query->where('server_content', 'like','%'.$params['keyword'].'%');
        }
        if (isset($params['shop_id']) && !empty($params['shop_id'])) {
            $query->where('shop_id', $params['shop_id']);
        }

        if (isset($params['member_id']) && !empty($params['member_id'])) {
            $query->where('member_id', $params['member_id']);
        }

        if(isset($params['date_time']) && !empty($params['date_time'])) {
            $timeZone = DateTime::toTimestamp('rangeMonthByDate', $params['date_time']);
            $query->whereBetween('fa_maintain_record.createtime', $timeZone);
        }
        return $query;
    }

    /**
     * 列表
     * @param array $params
     */
    public function index($params)
    {
        $params['with'] = ['shop'];
        $params['orderBy']   = 'createtime';
        $params['orderType'] = 'desc';
        return $this->getPageList($params)->each(function (&$item){
            $item['shop_name'] = $item['shop']['shop_name'];
            unset($item['shop']);
            return $item;
        });
    }

    /**
     * 新增记录
     * @param $params
     * @return bool
     */
    public function add($params)
    {
        // 写入
        $maintain = $this->model->create([
            'member_id'     => $params['member_id'],
            'car_numbers'    => $params['car_numbers'],
            'money'    => $params['money'],
            'remark'    => $params['remark'],
            'server_content'    => $params['server_content'],
            'mileage'    => $params['mileage'],
            'shop_id'    => $params['shop_id']
        ]);
        if (!empty($params['images'])){
            $images = [];
            foreach ($params['images'] as $v){
                $images[] = ['maintain_record_id'=>$maintain->id,'image'=>$v];
            }
            $this->imageModel->saveAll($images);
        }
        return true;
    }

    public function detail($params)
    {
        $info = MaintainRecord::get($params['id'],['shop','images']);
        $info['shop_name'] = '';
        if (!empty($info['shop'])){
            $info['shop_name'] = $info['shop']['shop_name'];
        }

        if (!empty($info['images'])) {
            $arr = [];
            foreach ($info['images'] as $v){
                $arr[] = $v['image'];
            }
            unset($info['images']);
            $info['images'] = $arr;
        }
        unset($info['shop']);
        return $info;
    }







}